package com.haoyeteng.sxbd.modular.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haoyeteng.sxbd.core.constant.DeleteFlag;
import com.haoyeteng.sxbd.core.constant.SxbdConstants;
import com.haoyeteng.sxbd.core.support.StrKit;
import com.haoyeteng.sxbd.core.util.MD5Util;
import com.haoyeteng.sxbd.core.util.NumUtil;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.model.Account;
import com.haoyeteng.sxbd.modular.business.dao.AccountMapper;
import com.haoyeteng.sxbd.modular.business.model.EleFence;
import com.haoyeteng.sxbd.modular.business.model.Product;
import com.haoyeteng.sxbd.modular.business.model.User;
import com.haoyeteng.sxbd.modular.business.service.IAccountService;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.haoyeteng.sxbd.modular.business.service.IEleFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 差分账号表 服务实现类
 * </p>
 *
 * @author haoyeteng123
 * @since 2019-05-25
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {

	@Autowired
	private AccountMapper accountMapper;

	@Autowired
	private IEleFenceService eleFenceService;
	
	@Override
	public List<Map<String, Object>> selectRtdAccounts(Page<Account> page, Map<String,Object> params) {
		return accountMapper.selectRtdAccounts(page, params);
	}

	@Override
	public List<Map<String, Object>> selectRtkAccounts(Page<Account> page, Map<String,Object> params) {
		return accountMapper.selectRtkAccounts(page, params);
	}

	@Override
	public List<Map<String, Object>> selectTaskAccounts(Page<Account> page, Map<String,Object> params) {
		return accountMapper.selectTaskAccounts(page, params);
	}

	@Override
	public Map<String, Object> selectByAccountId(Integer accountId) {
		return accountMapper.selectByAccountId(accountId);
	}

	@Override
	public Account addRtdAccount(Product product,Integer userId){

		EleFence eleFence = eleFenceService.selectOne(new EntityWrapper<EleFence>().orderBy("ele_fence_id").last("limit 1"));

		Account account = new Account();

		Integer count = this.selectCount(new EntityWrapper<Account>().eq("user_id", userId));
		String preAccount = "shy";
		String userIdFormat = NumUtil.autoGenericCode(userId, 4);
		String accountFormat = NumUtil.autoGenericCode(count + 1, 4);
		account.setAccount(preAccount+userIdFormat+accountFormat);

		Date date = new Date();
		account.setPassword(MD5Util.DEFULT_PASSWORD);
		account.setBindingState(SxbdConstants.ACCOUNT_BINDING_STATE_NO);
		account.setUserId(userId);
		account.setServiceType(product.getServiceType());
		account.setCreateTime(date);
		account.setUpdateTime(date);
		account.setFlag(DeleteFlag.OK.getCode());
		account.setEleFenceId(ToolUtil.isNotEmpty(eleFence) ? eleFence.getEleFenceId() : null);
		this.baseMapper.insert(account);
		return account;
	}

	@Override
	public List<Map<String, Object>> selectAccounts(Page<Account> page, Map<String,Object> params) {
		return this.baseMapper.selectAccounts(page, params);
	}

	@Override
	public List<Map<String, Object>> selectAccountForCurveByDay() {
		return this.baseMapper.selectAccountForCurveByDay();
	}
	@Override
	public List<Map<String, Object>> selectAccountForCurveByMonth() {
		return this.baseMapper.selectAccountForCurveByMonth();
	}
	@Override
	public List<Map<String, Object>> selectAccountForCurveByYear(){
		return this.baseMapper.selectAccountForCurveByYear();
	}

}
